package com.example.beat_x3.Adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.beat_x3.R;

import java.util.ArrayList;

import Entity.Inf;
import Entity.MyTime;

public class TimeAdapter extends RecyclerView.Adapter<TimeAdapter.MyViewHolder> {
    public ArrayList<MyTime> timeTable;
    private OnItemClickListener mOnItemClickListener;
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(String placeTimeNumber, String startTime,boolean status,ImageView image,int position);
    }

    public TimeAdapter(ArrayList<MyTime> timeTable) {
        this.timeTable = timeTable;
    }

    @NonNull
    @Override
    public TimeAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(Inf.context).inflate(R.layout.item_reservation, parent, false);
        return new MyViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.time.setText(timeTable.get(position).StartTime + "~~~" + timeTable.get(position).EndTime);
        if (timeTable.get(position).Status) {
            holder.image.setImageResource(R.drawable.green);
            holder.status=true;
            holder.placeTimeNumber=timeTable.get(position).PlaceTimeNumber;
            holder.startTime=timeTable.get(position).StartTime;
            holder.position=position;
        } else holder.image.setImageResource(R.drawable.red);
    }

    @Override
    public int getItemCount() {
        return timeTable.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView time;
        ImageView image;
        boolean status=false;
        String placeTimeNumber;
        String startTime;
        int position;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            time = itemView.findViewById(R.id.placetime_detail);
            image = itemView.findViewById(R.id.placetime_status);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(placeTimeNumber, startTime,status,image,position);
                    }
                }
            });
        }
    }
}
